
#include "rtthread.h"
#include "rthw.h"

#ifdef RT_USING_FINSH
#include "shell.h"
#endif

#include "board.h"
#include "init.h"
#include "led_key.h"
#include "adc.h"
#include "sysconfig.h"

static void led_blink(void *parameter)
{
    if(!(rt_tick_get() % 500))
    {
        hw_led_set(BOARD_LED_RED, LED_OP_TOGGLE);
    }
}

static void init_thread(void *parameter)
{
#ifdef RT_USING_CONSOLE
    console_init();
    rt_show_version();
#ifdef RT_USING_FINSH
    finsh_system_init();
#endif
#endif

    hw_led_init();
    rt_key_init();
    if (get_syscfg()->key_enable)
    {
        rt_key_enable();
    }

    rt_register_tick_hook(led_blink, RT_NULL);
    adc_timer_init();
    hw_usb_init();
    while(1)
    {
        rt_key_wait(RT_WAITING_FOREVER);
        if (rt_key_get_down(RT_KEY0))
        {
            rt_kprintf("key0 down\n");
        }
        if (rt_key_get_down(RT_KEY1))
        {
            rt_kprintf("key1 down\n");
        }
        if (rt_key_get_up(RT_KEY0))
        {
            rt_kprintf("key0 up\n");
        }
        if (rt_key_get_up(RT_KEY1))
        {
            rt_kprintf("key1 up\n");
        }
    }
}

void rt_application_init(void)
{
    rt_thread_t tid;
    tid = rt_thread_create("init", init_thread, RT_NULL, 1024, 1, 20);
    RT_ASSERT(tid != RT_NULL);
    rt_thread_startup(tid);
}
